package com.lanjiyin.cloud.common.rest;

import lombok.extern.slf4j.Slf4j;
import springfox.documentation.annotations.ApiIgnore;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.lanjiyin.cloud.common.biz.BaseBiz;
import com.lanjiyin.cloud.common.context.BaseContextHandler;
import com.lanjiyin.cloud.common.msg.ObjectRpcResponse;
import com.lanjiyin.cloud.common.msg.Result;
import com.lanjiyin.cloud.common.msg.TableRpcResponse;
import com.lanjiyin.cloud.common.util.ClientUtil;
import com.lanjiyin.cloud.common.util.Query;

import javax.servlet.http.HttpServletRequest;

import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

@Slf4j
public class BaseController<Biz extends BaseBiz,Entity> {
    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected Biz baseBiz;

    @ApiIgnore
    @RequestMapping(value = "",method = RequestMethod.POST)
    @ResponseBody
    public Result<ObjectRpcResponse<Entity>> add(@RequestBody Entity entity){
        baseBiz.insertSelective(entity);
        return new Result<ObjectRpcResponse<Entity>>(new ObjectRpcResponse<Entity>());
    }

    @ApiIgnore
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Result<ObjectRpcResponse<Entity>> get(@PathVariable int id){
        ObjectRpcResponse<Entity> entityObjectRestResponse = new ObjectRpcResponse<>();
        Object o = baseBiz.selectById(id);
        entityObjectRestResponse.data((Entity)o);
        return new Result<ObjectRpcResponse<Entity>>(entityObjectRestResponse);
    }
    
    @ApiIgnore
    @RequestMapping(value = "/id/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Result<ObjectRpcResponse<Entity>> getId(@PathVariable long id){
        ObjectRpcResponse<Entity> entityObjectRestResponse = new ObjectRpcResponse<>();
        Object o = baseBiz.selectById(id);
        entityObjectRestResponse.data((Entity)o);
        return new Result<ObjectRpcResponse<Entity>>(entityObjectRestResponse);
    }
    @ApiIgnore
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    @ResponseBody
    public Result<ObjectRpcResponse<Entity>> update(@RequestBody Entity entity){
        baseBiz.updateSelectiveById(entity);
        return new Result<ObjectRpcResponse<Entity>>(new ObjectRpcResponse<Entity>());
    }
    @ApiIgnore
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public Result<ObjectRpcResponse<Entity>> remove(@PathVariable int id){
        baseBiz.deleteById(id);
        return new Result<ObjectRpcResponse<Entity>>(new ObjectRpcResponse<Entity>());
    }
    @ApiIgnore
    @RequestMapping(value = "/id/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public Result<ObjectRpcResponse<Entity>> removeById(@PathVariable long id){
        baseBiz.deleteById(id);
        return new Result<ObjectRpcResponse<Entity>>(new ObjectRpcResponse<Entity>());
    }
    @ApiIgnore
    @RequestMapping(value = "/all",method = RequestMethod.GET)
    @ResponseBody
    public Result<List<Entity>> all(){
        return new Result<List<Entity>>(baseBiz.selectListAll());
    }
    @ApiIgnore
    @RequestMapping(value = "/page",method = RequestMethod.GET)
    @ResponseBody
    public Result<TableRpcResponse<Entity>> list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        return new Result<TableRpcResponse<Entity>>(baseBiz.selectByQuery(query));
    }
    public String getCurrentUserName(){
    	HttpServletRequest request = null;
		try {
			request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		}catch(Exception se) {}
		String hostIp = "";
		String name = "";
		String id = "";
		if(request!=null) {
			hostIp = StringUtils.defaultIfBlank(request.getHeader("userHost"), ClientUtil.getClientIp(request));
			name = StringUtils.trimToEmpty(request.getHeader("userName"));
			id = StringUtils.trimToEmpty(request.getHeader("userId"));
		}

		if (StringUtils.isBlank(name)) {
			name = BaseContextHandler.getUsername();
		}
		if (StringUtils.isBlank(id)) {
			id = BaseContextHandler.getUserID();
		}
        return name;
    }
}
